local skynet = require "skynet"
local mysql = require "skynet.db.mysql"
local utils = require "utils"

local CMD = {}
local db

local function connect(dbname,t)
	
end
--验证是否登陆
function CMD.checkLogin(user_info)
	local userid = user_info.userid or 0
	local sql_fmt = "SELECT userid,coin,nickname,sex,deviceid,headurl FROM users_info WHERE (userid=%d);"
	local select_sql = string.format(sql_fmt,userid)
	local res = db:query(select_sql)
	return res
end

--验证是否有设备码账号
function CMD.checkDeviceId(deviceid)
	deviceid = tostring(deviceid) or ""
	local res = nil
	if deviceid ~= "" then
		local sql_fmt = "SELECT userid,coin,nickname,sex,deviceid,headurl FROM users_info WHERE (deviceid='%s');"
		local select_sql = string.format(sql_fmt,deviceid)
		res = db:query(select_sql)
	end
	return res
end

--验证是否有openid账号
function CMD.checkOpenid(openid)
	openid = tostring(openid) or ""
	local res = nil
	if openid ~= "" then
		local sql_fmt = "SELECT userid,coin,nickname,sex,deviceid,headurl FROM users_info WHERE (openid='%s');"
		local select_sql = string.format(sql_fmt,openid)
		res = db:query(select_sql)
	end
	return res
end

--验证是否有openid账号
function CMD.checkUnionid(unionid)
	unionid = tostring(unionid) or ""
	local res = nil
	if unionid ~= "" then
		local sql_fmt = "SELECT userid,coin,nickname,sex,deviceid,headurl FROM users_info WHERE (unionid='%s');"
		local select_sql = string.format(sql_fmt,unionid)
		res = db:query(select_sql)
	end
	return res
end

--新增玩家
function CMD.insertUser(user_info)
	local userid = user_info.userid or 0
	local nickname = user_info.nickname or ""
	local deviceid = user_info.deviceid or ""
	local unionid = user_info.unionid or ""
	local sql_fmt = "INSERT INTO users_info (userid, nickname, deviceid, unionid, openid) VALUE (%d, '%s', '%s', '%s', '%s');"
	local insert_sql = string.format(sql_fmt,userid,nickname,deviceid, unionid, openid)
	db:query(insert_sql)
	local sql_fmt = "SELECT userid,coin,nickname,sex,deviceid,headurl FROM users_info WHERE (deviceid='%s');"
	local select_sql = string.format(sql_fmt,deviceid)
	res = db:query(select_sql)
	return res
end

--新增玩家unionid
function CMD.insertUserByUnionid(user_info)
	local userid = user_info.userid or 0
	local nickname = user_info.nickname or ""
	local deviceid = user_info.deviceid or ""
	local unionid = user_info.unionid or ""
	local sql_fmt = "INSERT INTO users_info (userid, nickname, deviceid, unionid, openid) VALUE (%d, '%s', '%s', '%s', '%s');"
	local insert_sql = string.format(sql_fmt,userid,nickname,deviceid, unionid, openid)
	db:query(insert_sql)
	local sql_fmt = "SELECT userid,coin,nickname,sex,deviceid,headurl FROM users_info WHERE (unionid='%s');"
	local select_sql = string.format(sql_fmt,unionid)
	res = db:query(select_sql)
	return res
end

--新增玩家openid
function CMD.insertUserByOpenid(user_info)
	local userid = user_info.userid or 0
	local nickname = user_info.nickname or ""
	local deviceid = user_info.deviceid or ""
	local unionid = user_info.unionid or ""
	local openid = user_info.openid or ""
	local sql_fmt = "INSERT INTO users_info (userid, nickname, deviceid, unionid, openid) VALUE (%d, '%s', '%s', '%s', '%s');"
	local insert_sql = string.format(sql_fmt,userid,nickname,deviceid, unionid, openid)
	db:query(insert_sql)
	local sql_fmt = "SELECT userid,coin,nickname,sex,deviceid,headurl FROM users_info WHERE (unionid='%s');"
	local select_sql = string.format(sql_fmt,unionid)
	res = db:query(select_sql)
	return res
end

function CMD.updateUserInfo()

end

function CMD.start()

end

function CMD.stop()

end

local function keep_alive()
	while true do
		skynet.error("mysqldb:  timer keep alive!")
		skynet.sleep(2*60*100)
		if db then
			db:query("set charset utf8")
		end
	end
end

skynet.start(function()
	local function on_connect(db)
		db:query("set charset utf8");
	end

	db = mysql.connect({
		host="sh-cynosdbmysql-grp-ps6uqx20.sql.tencentcdb.com",
	--	host="192.168.1.98",
		port=29227,
		database="dzgame",
		user="root",
		--password="123456",
		password="AKLjfjw/(123@jmdk",
		max_packet_size = 1024*1024,
		on_connect = on_connect
	})
	if not db then
		skynet.error("failed to connect db!")
		skynet.exit()
		return
	end

	skynet.dispatch("lua", function(session, source, cmd, ...)
		local f = assert(CMD[cmd])
		skynet.ret(skynet.pack(f(...)))
	end)
	skynet.error("mysqldb service start!")	
	skynet.fork(keep_alive)
end)
